#include "platformSystem.h"
#include "RyanMqttPlatform.h"

/**
 * @brief 申请内存
 *
 * @param size
 * @return void*
 */
void *platformMemoryMalloc(size_t size)
{
	return rt_malloc(size);
}

/**
 * @brief 释放内存
 *
 * @param ptr
 */
void platformMemoryFree(void *ptr)
{
	rt_free(ptr);
}

/**
 * @brief ms延时
 *
 * @param ms
 */
void platformDelay(uint32_t ms)
{
	rt_thread_mdelay(ms);
}

uint32_t platformUptimeMs(void)
{
#if (RT_TICK_PER_SECOND == 1000)
	return (uint32_t)rt_tick_get();
#else
	// 已经不能提供精准的ms了
	rt_tick_t tick = rt_tick_get();
	return (uint32_t)(((uint64_t)tick * 1000 + RT_TICK_PER_SECOND - 1) / RT_TICK_PER_SECOND);
#endif
}

/**
 * @brief 打印字符串函数,可通过串口打印出去
 *
 * @param str
 * @param strLen
 */
void platformPrint(char *str, uint16_t strLen)
{
	rt_kputs(str);
}

/**
 * @brief 初始化并运行线程
 *
 * @param userData
 * @param platformThread
 * @param name
 * @param entry
 * @param param
 * @param stackSize
 * @param priority
 * @return RyanMqttError_e
 */
RyanMqttError_e platformThreadInit(void *userData, platformThread_t *platformThread, const char *name,
				   void (*entry)(void *), void *const param, uint32_t stackSize, uint32_t priority)
{
	platformThread->thread = rt_thread_create(name,      // 线程name
						  entry,     // 线程入口函数
						  param,     // 线程入口函数参数
						  stackSize, // 线程栈大小
						  priority,  // 线程优先级
						  10);       // 线程时间片

	if (NULL == platformThread->thread)
	{
		return RyanMqttNoRescourceError;
	}

	rt_thread_startup(platformThread->thread);
	return RyanMqttSuccessError;
}

/**
 * @brief 销毁自身线程
 *
 * @param userData
 * @param platformThread
 * @return RyanMqttError_e
 */
RyanMqttError_e platformThreadDestroy(void *userData, platformThread_t *platformThread)
{
	rt_thread_delete(platformThread->thread);
	rt_schedule();
	return RyanMqttSuccessError;
}

/**
 * @brief 开启线程
 *
 * @param userData
 * @param platformThread
 * @return RyanMqttError_e
 */
RyanMqttError_e platformThreadStart(void *userData, platformThread_t *platformThread)
{
	rt_thread_resume(platformThread->thread);
	rt_schedule();
	return RyanMqttSuccessError;
}

/**
 * @brief 挂起线程
 *
 * @param userData
 * @param platformThread
 * @return RyanMqttError_e
 */
RyanMqttError_e platformThreadStop(void *userData, platformThread_t *platformThread)
{
	rt_thread_suspend(platformThread->thread);
	rt_schedule(); // rtthread挂起线程后应立即调用线程上下文切换函数
	return RyanMqttSuccessError;
}

/**
 * @brief 互斥锁初始化
 *
 * @param userData
 * @param platformMutex
 * @return RyanMqttError_e
 */
RyanMqttError_e platformMutexInit(void *userData, platformMutex_t *platformMutex)
{
	rt_mutex_init(&platformMutex->mutex, "mqttMutex", RT_IPC_FLAG_PRIO);
	return RyanMqttSuccessError;
}

/**
 * @brief 销毁互斥锁
 *
 * @param userData
 * @param platformMutex
 * @return RyanMqttError_e
 */
RyanMqttError_e platformMutexDestroy(void *userData, platformMutex_t *platformMutex)
{
	rt_mutex_detach(&platformMutex->mutex);
	return RyanMqttSuccessError;
}

/**
 * @brief 阻塞获取互斥锁
 *
 * @param userData
 * @param platformMutex
 * @return RyanMqttError_e
 */
RyanMqttError_e platformMutexLock(void *userData, platformMutex_t *platformMutex)
{
	rt_mutex_take(&platformMutex->mutex, RT_WAITING_FOREVER);
	return RyanMqttSuccessError;
}

/**
 * @brief 释放互斥锁
 *
 * @param userData
 * @param platformMutex
 * @return RyanMqttError_e
 */
RyanMqttError_e platformMutexUnLock(void *userData, platformMutex_t *platformMutex)
{
	rt_mutex_release(&platformMutex->mutex);
	return RyanMqttSuccessError;
}

/**
 * @brief 临界区初始化
 *
 * @param userData
 * @param platformCritical
 * @return RyanMqttError_e
 */
RyanMqttError_e platformCriticalInit(void *userData, platformCritical_t *platformCritical)
{
	return RyanMqttSuccessError;
}

/**
 * @brief 销毁临界区
 *
 * @param userData
 * @param platformCritical
 * @return RyanMqttError_e
 */
RyanMqttError_e platformCriticalDestroy(void *userData, platformCritical_t *platformCritical)
{
	return RyanMqttSuccessError;
}

/**
 * @brief 进入临界区
 *
 * @param userData
 * @param platformCritical
 * @return RyanMqttError_e
 */
RyanMqttError_e platformCriticalEnter(void *userData, platformCritical_t *platformCritical)
{
	platformCritical->level = rt_hw_interrupt_disable();
	return RyanMqttSuccessError;
}

/**
 * @brief 退出临界区
 *
 * @param userData
 * @param platformCritical
 * @return RyanMqttError_e
 */
RyanMqttError_e platformCriticalExit(void *userData, platformCritical_t *platformCritical)
{
	rt_hw_interrupt_enable(platformCritical->level);
	return RyanMqttSuccessError;
}
